package com.example.administrator.personalmail.Activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.eventBean.CarFragment;
import com.example.administrator.personalmail.fragment.FragmentFive;
import com.example.administrator.personalmail.fragment.FragmentFour;
import com.example.administrator.personalmail.fragment.FragmentOne;
import com.example.administrator.personalmail.fragment.FragmentThree;
import com.example.administrator.personalmail.fragment.FragmentTwo;
import com.example.administrator.personalmail.model.TabBean;
import com.example.administrator.personalmail.widget.FragmentTabHost;
import com.facebook.drawee.backends.pipeline.Fresco;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private FragmentTabHost fragmentTabHost;
    private ArrayList<TabBean> tabList = new ArrayList<>(5);
    private FragmentFour fragmentFour;
    private boolean isGetCarFragment;
    private boolean isExit = false;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            isExit = false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Fresco.initialize(this);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);

        initTab();


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mHandler.sendEmptyMessageDelayed(0, 2000);
            } else {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
                System.exit(0);
            }
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Subscribe
    public void getFragmentCar(CarFragment carFragment){
        fragmentFour = carFragment.getFragmentFour();
        isGetCarFragment = true;
    }

    private void initTab() {
        fragmentTabHost = findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
//                Toast.makeText(MainActivity.this,s+"",Toast.LENGTH_SHORT).show();
                if (s.equals("cart")){
                    if (isGetCarFragment){
                        fragmentFour.refresh();
                    }
                }
            }
        });

        TabBean one = new TabBean("home","主页",R.drawable.selector_icon_home, FragmentOne.class);
        TabBean two = new TabBean("hot","热卖",R.drawable.selector_icon_hot, FragmentTwo.class);
        TabBean three = new TabBean("category","分类",R.drawable.selector_icon_category, FragmentThree.class);
        TabBean four = new TabBean("cart","购物车",R.drawable.selector_icon_cart, FragmentFour.class);
        TabBean five = new TabBean("mine","我的",R.drawable.selector_icon_mine, FragmentFive.class);

        tabList.add(one);
        tabList.add(two);
        tabList.add(three);
        tabList.add(four);
        tabList.add(five);

        for (TabBean tabBean : tabList){
            View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator,null);
            ImageView tabImage = view.findViewById(R.id.icon_tab);

            TextView tabText = view.findViewById(R.id.text_indicator);
            TabHost.TabSpec  tabSpec = fragmentTabHost.newTabSpec(tabBean.getname());
            tabText.setText(tabBean.getTitle());
            tabImage.setBackgroundResource(tabBean.getImage());

            tabSpec.setIndicator(view);
            fragmentTabHost.addTab(tabSpec,tabBean.getFragment(),null);
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


}
